package com.fg114.main.recorder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.fg114.main.service.dto.JsonPack;
import com.fg114.main.service.http.A57HttpApiV3;
import com.fg114.main.util.CheckUtil;
import com.fg114.main.util.FileAccessI;
import com.rescueworkers.Settings;
import com.rescueworkers.dto.UploadVoiceOrImageInfo;

public class RecorderUploadUtil {
	private static final int SEND_BUFFER_SIZE100KB = 1024 * 100;
	private static final int SEND_BUFFER_SIZE50KB = 1024 * 50;
	private static final int SEND_BUFFER_SIZE20KB = 1024 * 20;

	/**
	 * 文件分段上传，100kb,50kb,20kb
	 * 
	 * @param resId
	 * @param uuId
	 * @param voiceIndex
	 */
	public static void cutFileUpload(UploadVoiceOrImageInfo info,
			String imageLengthList) {
		try {
			if (info.getPath() == null)
				return;

			FileAccessI fileAccessI = new FileAccessI(info.getPath(), 0);
			// 文件大小
			Long length = fileAccessI.getFileLength();
			// 每次读取大小
			int mBufferSize = SEND_BUFFER_SIZE100KB;
			byte[] buffer = new byte[mBufferSize];

			FileAccessI.Detail detail;
			int nRead = 0;// 实际真实读取大小
			long nStart = 0L;// 当前位置
			int errorCount = 0;// 连续错误次数
			int repeatRequestType = 0;
			boolean done = false;
			InputStream is;

			while (nStart < length) {
				long nStartTemp = nStart;

				// 每次读取的buffer
				detail = fileAccessI.getContent(nStart, mBufferSize);
				nRead = detail.length;
				buffer = detail.b;

				nStart += nRead;

				// 文件结束位
				if (nStart >= length) {
					done = true;
				}

				// 上传文件
				// try {
				// InputStream is = new ByteArrayInputStream(buffer);

				if (done) {
					byte[] buf = new byte[nRead];
					System.arraycopy(buffer, 0, buf, 0, nRead);
					// buf = buffer;
					is = new ByteArrayInputStream(buf);

				} else {
					is = new ByteArrayInputStream(buffer);
				}

				// 对参数判断
				JsonPack result = null;
				try {
					result = A57HttpApiV3.getInstance().postArrival(info.getId(),info.getPath(),info.getFileName(),
							imageLengthList, is,info.getLatitude(),info.getLongitude(),String.valueOf(info.getAcquisition_at()));;
				} catch (Exception e) {
					e.printStackTrace();
				}

				if (result != null && result.getRe() == Settings.CODE_SUCCESS) {
					errorCount = 0;

					// picUrl = result.getValue();

				} else {
					// 超时及其他情况
					errorCount++;
					if (errorCount < 3) {
						nStart = nStartTemp;

					} else {
						errorCount = 0;

						if (repeatRequestType == 0) {
							mBufferSize = SEND_BUFFER_SIZE50KB;
							repeatRequestType = 1;
						} else if (repeatRequestType == 1) {
							mBufferSize = SEND_BUFFER_SIZE20KB;
							repeatRequestType = 2;
						} else {
							break;
						}
					}
				}
			}
			fileAccessI.closeFile();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static InputStream getFileInputStream(String filePath) {
		InputStream is = null;
		try {
			if (CheckUtil.isEmpty(filePath))
				return null;
			is = new FileInputStream(filePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return is;
	}
}
